<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>cobar实现mysql分库分表 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>cobar实现mysql分库分表</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cobar 编译安装配置笔记:<a href="https://github.com/alibaba/cobar" target="_blank" rel="external">https://github.com/alibaba/cobar</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;windows下使用eclipse导入cobar项目,eclipse File -&gt; Import -&gt; Git :<a href="https://github.com/alibaba/cobar" target="_blank" rel="external">https://github.com/alibaba/cobar</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;linux下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget https://codeload.github.com/alibaba/cobar/zip/master</div></pre></td></tr></table></figure>
<p>F:\mycat&gt;mvn compile<br>F:\mycat&gt;mvn package</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;生成压缩包cobar-server-1.2.7.tar.gz，放到linux环境中解压出来，没有logs目录，新建并运行查看目录结构如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">[root@XAYQ-Test3 ~]<span class="comment"># tree /opt/cobar-server  </span></div><div class="line">/opt/software/cobar-server  </div><div class="line">├── bin  </div><div class="line">│   ├── restart.sh  </div><div class="line">│   ├── shutdown.sh  </div><div class="line">│   ├── startup.bat  </div><div class="line">│   └── startup.sh  </div><div class="line">├── conf  </div><div class="line">│   ├── log4j.xml                       <span class="comment">#日志配置文件，不需要修改  </span></div><div class="line">│   ├── rule.xml                        <span class="comment">#mysql路由规则  </span></div><div class="line">│   ├── schema.xml                      <span class="comment">#  </span></div><div class="line">│   └── server.xml                      <span class="comment">#  </span></div><div class="line">├── lib  </div><div class="line">│   ├── cobar-server-1.2.7.jar  </div><div class="line">│   └── log4j-1.2.17.jar  </div><div class="line">└── logs  </div><div class="line">    ├── alarm.log  </div><div class="line">    ├── console.log  </div><div class="line">    ├── heartbeat.log  </div><div class="line">    ├── stdout.log  </div><div class="line">    └── stdout.log.2014-07-10</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.主要修改以下几个文件：rule.xml </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<!-- 路由规则定义，定义什么表，什么字段，采用什么路由算法 -->  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div></pre></td><td class="code"><pre><div class="line">&lt;tableRule name=<span class="string">"rule1"</span>&gt;  </div><div class="line">  &lt;rule&gt;  </div><div class="line">    &lt;columns&gt;id&lt;/columns&gt;  </div><div class="line">    &lt;algorithm&gt;&lt;![CDATA[ func1(<span class="variable">$&#123;id&#125;</span>) ]]&gt;&lt;/algorithm&gt;  </div><div class="line">  &lt;/rule&gt;  </div><div class="line">&lt;/tableRule&gt;  </div><div class="line">  </div><div class="line">&lt;!-- 路由函数定义 --&gt;  </div><div class="line">&lt;<span class="keyword">function</span> name=<span class="string">"func1"</span> class=<span class="string">"com.alibaba.cobar.route.function.PartitionByLong"</span>&gt;  </div><div class="line">  &lt;property name=<span class="string">"partitionCount"</span>&gt;2&lt;/property&gt;  </div><div class="line">  &lt;property name=<span class="string">"partitionLength"</span>&gt;512&lt;/property&gt;  </div><div class="line">&lt;/<span class="keyword">function</span>&gt;  </div><div class="line">```  </div><div class="line"></div><div class="line">&amp;<span class="comment">#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;2.schema.xml ，定义数据节点</span></div><div class="line"></div><div class="line">```bash</div><div class="line">&lt;cobar:schema xmlns:cobar=<span class="string">"http://cobar.alibaba.com/"</span>&gt;  </div><div class="line">  </div><div class="line">  &lt;!-- schema定义 --&gt;  </div><div class="line">  &lt;schema name=<span class="string">"cppdb"</span> dataNode=<span class="string">"cppDb1"</span>&gt;  </div><div class="line">    &lt;table name=<span class="string">"tb2"</span> dataNode=<span class="string">"cppDb2,cppDb3"</span> rule=<span class="string">"rule1"</span> /&gt;  </div><div class="line">  &lt;/schema&gt;  </div><div class="line">  </div><div class="line">  &lt;!-- 数据节点定义，数据节点由数据源和其他一些参数组织而成。--&gt;  </div><div class="line">  &lt;dataNode name=<span class="string">"cppDb1"</span>&gt;  </div><div class="line">    &lt;property name=<span class="string">"dataSource"</span>&gt;  </div><div class="line">      &lt;dataSourceRef&gt;cppDataSource[0]&lt;/dataSourceRef&gt;  </div><div class="line">    &lt;/property&gt;  </div><div class="line">  &lt;/dataNode&gt;  </div><div class="line">  &lt;dataNode name=<span class="string">"cppDb2"</span>&gt;  </div><div class="line">    &lt;property name=<span class="string">"dataSource"</span>&gt;  </div><div class="line">      &lt;dataSourceRef&gt;cppDataSource[1]&lt;/dataSourceRef&gt;  </div><div class="line">    &lt;/property&gt;  </div><div class="line">  &lt;/dataNode&gt;  </div><div class="line">  &lt;dataNode name=<span class="string">"cppDb3"</span>&gt;  </div><div class="line">    &lt;property name=<span class="string">"dataSource"</span>&gt;  </div><div class="line">      &lt;dataSourceRef&gt;cppDataSource[2]&lt;/dataSourceRef&gt;  </div><div class="line">    &lt;/property&gt;  </div><div class="line">  &lt;/dataNode&gt;  </div><div class="line">  </div><div class="line">  &lt;!-- 数据源定义，数据源是一个具体的后端数据连接的表示。--&gt;  </div><div class="line">  &lt;dataSource name=<span class="string">"cppDataSource"</span> <span class="built_in">type</span>=<span class="string">"mysql"</span>&gt;  </div><div class="line">    &lt;property name=<span class="string">"location"</span>&gt;  </div><div class="line">      &lt;location&gt;172.22.14.7:3306/cpp1&lt;/location&gt;  </div><div class="line">      &lt;location&gt;172.22.14.7:3306/cpp2&lt;/location&gt;  </div><div class="line">      &lt;location&gt;172.22.14.7:3306/cpp3&lt;/location&gt;  </div><div class="line">    &lt;/property&gt;  </div><div class="line">    &lt;property name=<span class="string">"user"</span>&gt;root&lt;/property&gt;  </div><div class="line">    &lt;property name=<span class="string">"password"</span>&gt;root&lt;/property&gt;  </div><div class="line">    &lt;property name=<span class="string">"sqlMode"</span>&gt;STRICT_TRANS_TABLES&lt;/property&gt;  </div><div class="line">  &lt;/dataSource&gt;  </div><div class="line">  </div><div class="line">&lt;/cobar:schema&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;3.server.xml，定义cobar对外统一的数据接口。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">  &lt;!-- 用户访问定义，用户名、密码、schema等信息。 --&gt;</div><div class="line"></div><div class="line"></div><div class="line">&lt;user name=<span class="string">"root"</span>&gt;  </div><div class="line">&lt;property name=<span class="string">"password"</span>&gt;root&lt;/property&gt;  </div><div class="line">&lt;property name=<span class="string">"schemas"</span>&gt;cppdb&lt;/property&gt;  </div><div class="line">t;/user&gt;</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/MySQL/28. cobar实现mysql分库分表/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
